package com.finalproject.entity;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
/**
 * Representation for CafeDatabase
 * 
 *
 */
public class CafeDatabase {
	
	/////NOTE: remove by id?

	private List<Cafe> cafes;
	private static CafeDatabase INSTANCE;
	Gson gson=new Gson();
	
	public static CafeDatabase getInstance() {
		if(INSTANCE == null) {
			INSTANCE = new CafeDatabase();
		}
		return INSTANCE;
	}
	
	private CafeDatabase() {
		this.cafes = new ArrayList<Cafe>();
		//initFromMemory();
	
		try{
		initFromFile();
		}
		catch (IOException e){
			e.printStackTrace();
		}
		
	}
	
	
	public List<Cafe> getCafes() {
		return cafes;
	}
	
	public Cafe getIndex(int index) {
		return cafes.get(index);
	}
	
	private void initFromFile() throws IOException{
		String str=readFile("cafe_database.txt");
		Type myType=new TypeToken<List<Cafe>>() {}.getType();
		cafes=gson.fromJson(str, myType);
	
	}
	private String readFile(String pathname) throws IOException {

	    File file = new File(pathname);
	    StringBuilder fileContents = new StringBuilder((int)file.length());
	    Scanner scanner = new Scanner(file);
	    String lineSeparator = System.getProperty("line.separator");

	    try {
	        while(scanner.hasNextLine()) {        
	            fileContents.append(scanner.nextLine() + lineSeparator);
	        }
	        return fileContents.toString();
	    } finally {
	        scanner.close();
	    }
	}
	private void initFromMemory() {
		Cafe v1 = new Cafe(101,new int[] {220,125});
		v1.add(new CafeItem("KitKat", 1.00, "KitKatis a chocolate-coated wafer confection ", 218, 11.0,5.0, 27.0, 23.0,3.0,3));
		v1.add(new CafeItem("Snickers", 1.00, "Handle your Hunger", 271, 13.6,7.0, 34.5,140.0,3.0,3));
		v1.add(new CafeItem("Nature Valley", 1.00, "A Healter Bar", 180, 6.0,0.0, 29.0,160.0,4.0,3));
		
		v1.add(new CafeItem("Planters Peanut Dry Roasted", 1.00, "Delecious snackbar", 160, 5.0, 0.0, 27.0, 190.0,7.0,10));
		v1.add(new CafeItem("Sunchips Original", 1.00, "A Healthy Chips", 137, 6.0,0.0, 19.0, 92.0,2.0,10));
		v1.add(new CafeItem("M&M Candy", 1.00, "Delecious snackbar", 138, 6.0, 20.0, 5.0,17.0,1.0,2));
		
		v1.add(new CafeItem("OREOS", 1.20, "Chocolate Cookies", 260, 13.0, 31.0,5.0, 31.0,5.0,20));
		v1.add(new CafeItem("Lays Chips", 1.00, "Original Lay Chips", 150, 10.0, 3.0, 15.0, 180.0,2.0,20));
		v1.add(new CafeItem("Ritz", 1.00, "Ritz Crackers Original", 80, 5.0, 10.0, 4.0,105.0,1.0,30));
//		
//		
		cafes.add(v1);
		Cafe v2 = new Cafe(102,new int[] {20,250});
		
		v2.add(new CafeItem("OREOS", 1.20, "Chocolate Cookies", 260, 13.0, 31.0,0.0, 31.0,5.0,30));
		v2.add(new CafeItem("Lays Chips", 1.00, "Original Lay Chips", 150, 10.0, 15.0,0.0, 180.0,2.0,20));
		v2.add(new CafeItem("Ritz", 1.00, "Ritz Crackers Original", 80, 5.0, 10.0, 3.0,105.0,1.0,20));
//		
		v2.add(new CafeItem("Nabisco Chip Ahoy", 1.00, " Nabisco Chips Ahoy ~ Thin Crisps", 100, 3.0, 0.0, 18.0, 140.0,1.0,30));
		v2.add(new CafeItem("DASANI Spring Winter ", 1.00, "Water, bottled, non-carbonated, DASANI", 0, 0.0, 0.0,0.0, 0.0,0.0,30));
		v2.add(new CafeItem("Diet Code ", 1.00, "Diet Code", 0, 0.0, 0.0,0.0, 30.0,0.0,20));
	
		cafes.add(v2);
		
		
		
		Cafe v3 = new Cafe(103,new int[] {250,230});
		v3.add(new CafeItem("KitKat", 1.00, "KitKatis a chocolate-coated wafer confection ", 218, 11.0,5.0, 27.0, 23.0,3.0,20));
		v3.add(new CafeItem("Snickers", 1.00, "Handle your Hunger", 271, 13.6,7.0, 34.5,140.0,3.0,20));
		v3.add(new CafeItem("Nature Valley", 1.00, "A Healter Bar", 180, 6.0,0.0, 29.0,160.0,4.0,20));
		
		v3.add(new CafeItem("Planters Peanut Dry Roasted", 1.00, "Delecious snackbar", 160, 5.0, 0.0, 27.0, 190.0,7.0,20));
		v3.add(new CafeItem("Sunchips Original", 1.00, "A Healthy Chips", 137, 6.0,0.0, 19.0, 92.0,2.0,20));
		v3.add(new CafeItem("M&M Candy", 1.00, "Delecious snackbar", 138, 6.0, 20.0, 5.0,17.0,1.0,20));
		
		v3.add(new CafeItem("OREOS", 1.20, "Chocolate Cookies", 260, 13.0, 31.0,5.0, 31.0,5.0,20));
		v3.add(new CafeItem("Lays Chips", 1.00, "Original Lay Chips", 150, 10.0, 3.0, 15.0, 180.0,2.0,10));
		v3.add(new CafeItem("Ritz", 1.00, "Ritz Crackers Original", 80, 5.0, 10.0, 4.0,105.0,1.0,20));
//		
//		
		cafes.add(v3);
		Cafe v4 = new Cafe(104,new int[] {430,260});
		
		v4.add(new CafeItem("OREOS", 1.20, "Chocolate Cookies", 260, 13.0, 31.0,0.0, 31.0,5.0,30));
		v4.add(new CafeItem("Lays Chips", 1.00, "Original Lay Chips", 150, 10.0, 15.0,0.0, 180.0,2.0,30));
		v4.add(new CafeItem("Ritz", 1.00, "Ritz Crackers Original", 80, 5.0, 10.0, 3.0,105.0,1.0,10));
//		
		v4.add(new CafeItem("Nabisco Chip Ahoy", 1.00, " Nabisco Chips Ahoy ~ Thin Crisps", 100, 3.0, 0.0, 18.0, 140.0,1.0,20));
		v4.add(new CafeItem("DASANI Spring Winter ", 1.00, "Water, bottled, non-carbonated, DASANI", 0, 0.0, 0.0,0.0, 0.0,0.0,20));
		v4.add(new CafeItem("Diet Code ", 1.00, "Diet Code", 0, 0.0, 0.0,0.0, 30.0,0.0,10));
	
		cafes.add(v4);
		
		
		
	}
	
	
	public void add(Cafe Cafe) {
		cafes.add(Cafe);
	}
	
	
	public void remove(Cafe Cafe) {
		cafes.remove(Cafe);
	}
}
